package com.focus.wecenter.dao.impl;

import com.focus.wecenter.dao.TopicDao;
import com.focus.wecenter.pojo.Topic;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 话题数据库操作实现
 *
 * @author Lv Jing
 * @date 2018年01月06日 14:15
 */
@Repository
public class TopicDaoImpl extends BaseDaoImpl<Topic> implements TopicDao {

    @Override
    public List<Topic> addTopics(String... names) {
        if (names == null) {
            return null;
        }
        List<Topic> result = new ArrayList<>();
        for (String name: names) {
            Topic topic = new Topic();
            topic.setTopicName(name);
            this.getHibernateTemplate().save(topic);
            result.add(topic);
        }
        return result;
    }

    @Override
    public Topic findByTopicName(String topicName) {
        List<?> list = this.getHibernateTemplate().find("from Topic where topicName = ?", topicName);
        if (list != null && list.size() > 0) {
            return (Topic) list.get(0);
        }
        return null;
    }
}
